/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2022  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "gctl/core.h"
#include "gctl/io.h"
#include "gctl/potential.h"

using namespace gctl;

int main(int argc, char const *argv[])
{
	try
	{
		// set up observation points
		array<point3ds> obes;
		gridspace(point3ds(550, 20, 0), point3ds(550, 70, 0), 
			point3ds(550, 0, 10), point3ds(550, 0, 60), 101, 101, obes);

		gmshio fio("data/tess/tess_sorted", Input);

		array<vertex3dc> nodes;
		array<grav_triangle> eles;
		fio.read_mesh(eles, nodes);

		array<gravtri_para> gpara(eles.size());
		callink_gravity_para(eles, gpara);

		array<double> out_data;
		array<point3dc> out_grad;
		array<tensor> out_tens;

		gobser(out_data, eles, obes, 1.0, ShortMsg);
		save_netcdf_grid("data/tess/tess_gravity", out_data, 101, 101, 20.0, 0.5, 10.0, 0.5, "easting", "northing", "potential");


		gobser(out_grad, eles, obes, 1.0, ShortMsg);
		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_grad[i].x;
		}
		append_netcdf_grid("data/tess/tess_gravity", out_data, "easting", "northing", "gravity_r");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_grad[i].y;
		}
		append_netcdf_grid("data/tess/tess_gravity", out_data, "easting", "northing", "gravity_t");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_grad[i].z;
		}
		append_netcdf_grid("data/tess/tess_gravity", out_data, "easting", "northing", "gravity_p");


		gobser(out_tens, eles, obes, 1.0, ShortMsg);
		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(0, 0);
		}
		append_netcdf_grid("data/tess/tess_gravity", out_data, "easting", "northing", "tensor_rr");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(0, 1);
		}
		append_netcdf_grid("data/tess/tess_gravity", out_data, "easting", "northing", "tensor_rt");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(0, 2);
		}
		append_netcdf_grid("data/tess/tess_gravity", out_data, "easting", "northing", "tensor_rp");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(1, 0);
		}
		append_netcdf_grid("data/tess/tess_gravity", out_data, "easting", "northing", "tensor_tr");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(1, 1);
		}
		append_netcdf_grid("data/tess/tess_gravity", out_data, "easting", "northing", "tensor_tt");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(1, 2);
		}
		append_netcdf_grid("data/tess/tess_gravity", out_data, "easting", "northing", "tensor_tp");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(2, 0);
		}
		append_netcdf_grid("data/tess/tess_gravity", out_data, "easting", "northing", "tensor_pr");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(2, 1);
		}
		append_netcdf_grid("data/tess/tess_gravity", out_data, "easting", "northing", "tensor_pt");

		for (int i = 0; i < obes.size(); ++i)
		{
			out_data[i] = out_tens[i].at(2, 2);
		}
		append_netcdf_grid("data/tess/tess_gravity", out_data, "easting", "northing", "tensor_pp");
	}
	catch (std::exception &e)
	{
		GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
	}
	return 0;
}